let Controller = {
  mounted() {
    this.initData();
  },
  activated() {
    this.initData();
    this.requestDetail();
  },
  methods: {
    initData() {
      const param = JSON.parse(this.$route.query.data);
      this.Model.queryData = _.cloneDeep(param);
      this.Model.detailFlag = Boolean(param.flag);
    },
    onUploadFileChange(e) {
      if (e.target.files.length == 0) {
        return;
      }
      this.Model.uploadFile = e.target.files[0];
    },
    onEditCount(index){
      let id = this.Model.detailData[index].id;
      if(!id){
        this.Model.detailData[index].id=0;
        return;
      }
      this.Model.detailData[index].id = parseFloat(id).toFixed(1);
    },
    getFilterData(val) {
      this.Model.searchFromData = val;
      console.log(this.Model.searchFromData)
    },
    requestDetail() {
      const whInventoryMainId = this.Model.queryData.id;
      const param = {
        condition: {
          whInventoryMainId
        }
      }
      if (whInventoryMainId) this.$ajax
      .post(`/api/whInventoryDetails/list`, param)
      .then(response => {
        const data = response.data;
        if (data.status == 200) {
          const result = data.result;
          this.Model.detailData = result.detailsList;
          this.Model.filterOption.items[0].data = result.countingNumber;
          this.Model.filterOption.items[1].data = result.sapCountingNumber;
          this.Model.filterOption.items[2].data = result.inventoryWarehouse;
          this.Model.filterOption.items[3].data = result.createBy;
          this.Model.filterOption.items[4].data = result.inventoryStartTime;
          this.Model.hasBeentotal = result.hasBeentotal;
          this.Model.totalNumber = result.totalNumber;
          this.Model.pager.pageTotal = data.result.total;
          this.Model.pager.pageIndex = data.result.pageIndex;
        } else {
          this.$message.error(data.msg);
        }

      })
      .catch(error => {
        this.$message.error('请求错误');
      });
    },
    toBarcodeDetail(row) {
      //跳转条码详情
      const name = 'purchaseWarehousingBarcodeDetail';
      this.$router.push({
        name: name,
        query: {
          data: row
        }
      })
    },
    setStepData(list, status) {
      let activeStep = 0,
      step = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].code == status) activeStep = i;
        step.push({
          title: list[i].codeName,
          description: list[i].createTime
        })
      };
      this.Model.stepOption = {
        activeStep,
        step
      }
    },

    onExportMaterial() {
      this.Model.dialogFormVisible = true;
    },
    onSureWarehousing() {
      const list = this.Model.exportTableSelectList || [];
      if (list.length == 0) return;
      const ids = list.map(item => {
        return item.id
      });
      if (!this.Model.searchFromData) {
        this.$message.warning('请选择入库仓库！');
        return;
      }
      const param = {
        ids,
        whWarehouseId: this.Model.whWarehouseId,
        locationId: this.Model.searchFromData.locationName,
      }
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/inWarehouse`, param)
      .then(res => {
        const data = res.data;
        if (data.status == 200) {
          this.$message.success('入库成功！');
          this.$router.push({
            name: `purchaseWarehousing`,
          })
        } else {
          this.$message.error(data.msg)
        }
      }).catch(err => {
        this.$message.error('请求失败');
      })
    },
    onTableCurrentChange(val) {
      //表格分页-当前页变动操作
      this.Model.pager.params.pageIndex = val;
      this.requestDetail();
    },
    onTableSizeChange(val) {
      //表格分页-页数量变动操作
      this.Model.pager.params.pageSize = val;
      this.requestDetail();
    },
    onCardClick(color) {
      //页面card搜索点击操作
      this.Model.cardSelected = color;
    },
    onTableSelectionChange(val, index, a) {
      this.Model.exportTableSelectList = val;
    },
    onUploadSureFile() {
      /*  */
      const file = this.Model.uploadFile;
      const number = this.Model.queryData.number;
      let fileName;
      if (file && !file.name) {
        this.$message({
          type: 'error',
          message: '还没有选择要上传的文件!'
        });
        return;
      } else {
        const extension = file.name.split('.')[1] === 'xls';
        const extension2 = file.name.split('.')[1] === 'xlsx';
        const isLt2M = file.size / 1024 / 1024 < 5
        if (!extension && !extension2) {
          this.$message.warning('上传模板只能是 xls、xlsx格式!')
          return;
        }
        fileName = file.name;
      }
      let fileFormData = new FormData();
      fileFormData.append('file', file, fileName); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      fileFormData.append('number', number); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      let requestConfig = {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
      }
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importPurchaseWarehouseExcel`, fileFormData, requestConfig)
      // this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importPurchaseWarehouseExcel`, fileFormData, requestConfig)
      .then(res => {
        const data = res.data;
        if (data.status == 200) {
          this.$message({
            message: '上传成功',
            type: 'success',
          });
          this.Model.detailData.actualSaleGoodsInfoVos = data.result;
          this.Model.dialogFormVisible = false;
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    cardClick(color) {
      this.Model.cardSelected = color;
    },
    showChild(val) {
      this.Model.searchFromData = val;
    },
    toSearch(val) {
      console.log(val)
    },
    toClear(val) {
      this.Model.searchFromData = this.Model.defaultSearchFromData;
    },
    toExport(val) {
      console.log(val);
    },
    onTabChange(val) {
      switch (val) {
        case 1:
        break;
        case 2:
        break;
        case 3:
        break;
      }
    },
    requestSaleGoodsInfoByEntity() {
      const param = {
        batchNo: this.Model.exportBarcode
      };
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/getByKeyWord`, param)
      .then(res => {
        const data = res.data;

        if (data.status == 200) {
          const result = data.result;
          this.Model.detailData.actualSaleGoodsInfoVos = [result];
        }
      }).catch(err => {
        console.log(err)
      })
    },
    toggleTableDetail(val) {
      const obj = this.Model[val];
      if (obj.flag === true) {
        this.Model[val].src = require('@/assets/img/icon_collapse.png');
        this.Model[val].flag = false;
      } else {
        this.Model[val].src = require('@/assets/img/icon_unfold.png');
        this.Model[val].flag = true;
      }
    },
    onDialogEdit(flag, val) {
      this.Model.editDialogFlag = flag;
      if (flag === false) return;
      const obj = this.Model[val];
      this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
      this.getAddress();
    },
    changeAddress(val) {
      if (!val) {
        this.getAddress();
      }
    },
    getAddress() {
      const idcard = 4310555;
      this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idcard}`, {})
      .then(response => {
        const data = response.data;
        if (data.status == 200) {
          this.Model.addressList = data.result;
        }
      }).catch(err => {
        console.log(err)
      })
    },
    updateBaseSaleOrder() {
      const data = this.Model.editDialogFormData;
      let params;
      if (data.shippingMethod === '1') {
        params = {
          shippingMethod: data.shippingMethod,
          shipper: data.shipper,
          originChannel: data.originChannel,
          salesman: data.salesman,
        }
      } else {
        params = data;
      }
      this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
      .then(response => {
        const data = response.data;
        if (data.status == 200) {
          this.onDialogEdit(false);
          this.$message({
            type: 'success',
            message: '已成功保存'
          });
          this.requestDetail();
        } else {
          this.$message.error(data.msg);
        }
      }).catch(err => {
        this.$message.error(err);
        this.onDialogEdit(false);
      });
    },
  }
}
export default Controller;
